package org.xyl.chat.engine;
// 建议dashscope SDK的版本 >= 2.12.0
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

import java.util.Map;


public class DashscopeChatEngine {


    private static String appId ="9f4e7b25e6bd4023b7f679b82ef34754";
    private static String appKey ="sk-d9f42afda5924894939fbca6f80b9876";

    public static class Main {
        public static void appCall()
                throws ApiException, NoApiKeyException, InputRequiredException {
//            testEnv();
            String dashscopeApiKey1 = System.getProperty("DASHSCOPE_API_KEY");
            System.out.println("dashscopeApiKey1: " + dashscopeApiKey1);
            String dashscopeApiKey = System.getenv("DASHSCOPE_API_KEY");
            System.out.println("dashscopeApiKey: " + dashscopeApiKey);
            dashscopeApiKey = appKey;
            ApplicationParam param = ApplicationParam.builder()
                    // 若没有配置环境变量，可用百炼API Key将下行替换为：.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中，以减少API Key泄露风险。
                    .apiKey(dashscopeApiKey)
                    .appId(appId)
                    .prompt("你是谁？")
                    .build();

            Application application = new Application();
            ApplicationResult result = application.call(param);

            System.out.printf("text: %s\n",
                    result.getOutput().getText());
        }

        public static void main(String[] args) {
            try {
                appCall();
            } catch (ApiException | NoApiKeyException | InputRequiredException e) {
                System.err.println("message："+e.getMessage());
                System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
            }
            System.exit(0);
        }

        public static void testEnv(){
            Map<String, String> env = System.getenv();
            env.keySet().stream()
                    .sorted()
                    .forEach(key -> System.out.println(key+" ： " + env.get(key)));
        }
    }
}
